package com.example.our_three;

import android.Manifest;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.navigation.NavigationView;
import com.tbruyelle.rxpermissions2.RxPermissions;


public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private Toolbar mToolbar;
    private HomeFragment homeFragment;
    private SettingsFragment settingsFragment;
    private AboutFragment aboutFragment;

    private final int HOME = 1;
    private final int SETTINGS = 2;
    private final int ABOUT = 3;
    private void getPermissions0() {
        RxPermissions rxPermissions0 = new RxPermissions(this);
        rxPermissions0.request(Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe();
    }
    private void getPermissions1() {
        RxPermissions rxPermissions1 = new RxPermissions(this);
        rxPermissions1.request(Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS)
                .subscribe();
    }
    private void getPermissions2() {
        RxPermissions rxPermissions2 = new RxPermissions(this);
        rxPermissions2.request(Manifest.permission.INTERNET,
                Manifest.permission.INTERNET)
                .subscribe();
    }
    private void getPermissions3() {
        RxPermissions rxPermissions3 = new RxPermissions(this);
        rxPermissions3.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe();
    }
    private void getPermissions4() {
        RxPermissions rxPermissions4 = new RxPermissions(this);
        rxPermissions4.request(Manifest.permission.SEND_SMS,
                Manifest.permission.SEND_SMS)
                .subscribe();
    }
    private void getPermissions5() {
        RxPermissions rxPermissions5 = new RxPermissions(this);
        rxPermissions5.request(Manifest.permission.SYSTEM_ALERT_WINDOW,
                Manifest.permission.SYSTEM_ALERT_WINDOW)
                .subscribe();
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SEND_SMS} , 1);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getPermissions0();
        getPermissions1();
        getPermissions2();
        getPermissions3();
        getPermissions4();
        getPermissions5();
        new AlertDialog.Builder(this)
                .setTitle("权限提示")
                .setMessage("请手动开启”悬浮窗和发送短信的权限")
                .setPositiveButton("确定", null).show();
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("请先进入设置，设置您的相关信息，并选择一个铃声！")
                .setPositiveButton("确定", null).show();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);


        mNavigationView = (NavigationView) findViewById(R.id.navigation_view);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        //为activity窗口设置活动栏
        setSupportActionBar(mToolbar);

        final ActionBar actionBar = getSupportActionBar();
        //设置导航图标
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        actionBar.setDisplayHomeAsUpEnabled(true);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        if (mNavigationView != null) {
            setupDrawerContent(mNavigationView);
        }

//        switchToHome();
        showFragment(HOME);

    }

    private void setupDrawerContent(NavigationView navigationView) {
        //监听navigationView的项目选择
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.navigation_item_home:
//                                switchToHome();
                                showFragment(HOME);
                                mToolbar.setTitle(R.string.app_name);
                                break;
                            case R.id.navigation_item_place:
                                switchToPlace();
                                break;
                            case R.id.navigation_item_settings:
//                                switchToSettings();
                                showFragment(SETTINGS);
                                mToolbar.setTitle(R.string.navigation_settings);
                                break;
                            case R.id.navigation_item_about:

                                showFragment(ABOUT);
                                mToolbar.setTitle(R.string.navigation_about);
                                break;
                        }
                        item.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
    }


    private void switchToPlace() {

        mToolbar.setTitle(R.string.app_name);
        startActivity(new Intent(this, MapActivity.class));

    }


    public void showFragment(int index){
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        //想要显示一个fragment，先隐藏所有fragment，防止重叠
        hideFragment(ft);

        switch (index){
            case HOME:
                if(homeFragment != null){
                    ft.show(homeFragment);
                }else{
                    homeFragment = new HomeFragment();
                    ft.add(R.id.frame_content, homeFragment);
                }
                break;
            case SETTINGS:
                if(settingsFragment != null){
                    ft.show(settingsFragment);
                }else{
                    settingsFragment = new SettingsFragment();
                    ft.add(R.id.frame_content, settingsFragment);
                }
                break;
            case ABOUT:
                if(aboutFragment != null){
                    ft.show(aboutFragment);
                }else{
                    aboutFragment = new AboutFragment();
                    ft.add(R.id.frame_content, aboutFragment);
                }
                break;
        }
        ft.commit();
    }
    /*
    当fragment已被实例化，就隐藏起来
     */
    public void hideFragment(FragmentTransaction ft){
        if(homeFragment != null){
            ft.hide(homeFragment);
        }
        if(settingsFragment != null){
            ft.hide(settingsFragment);
        }
        if(aboutFragment != null){
            ft.hide(aboutFragment);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if(item.getItemId() == android.R.id.home)
        {
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true ;
        }
        if(item.getItemId() == R.id.id_menu_settings){
            showFragment(SETTINGS);
        }
        return super.onOptionsItemSelected(item);
    }

}
